package com.xjnt.mnui.service;

import java.util.List;

import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.frameworks.plugin.sql.criterion.Restrictions;
import com.xjnt.frameworks.plugin.sql.criterion.SqlCriteria;
import com.xjnt.mnui.entity.Portal;

@Service
public class PortalService extends BaseService<Portal> {

	@Override
	public Portal getModel() {
		return Portal.me;
	}

	public boolean updateDefalut(String uuid){
		List<Portal> portals = findAll();
		for (Portal portal : portals) {
			portal.set(Portal.IS_DEFAULT, 0);
			portal.update();
		}
		Portal portal = findById(uuid);
		portal.set(Portal.IS_DEFAULT, 1);
		return portal.update();
	}
	
	public Portal findByDefault(){
		SqlCriteria criteria = new SqlCriteria(getModel());
		criteria.add(Restrictions.eq(Portal.IS_DEFAULT, 1));
		return getModel().findFirst(criteria.toSql(), 1);
	}
}
